package com.msxt.controller;

import com.msxt.dto.ChatMessage;
import com.msxt.result.R;
import com.msxt.service.ChatMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatMessageService chatMessageService;

    @PostMapping("/saveMessage")
    public R saveMessage(@RequestBody ChatMessage chatMessage) {
        try {
            Long messageId = chatMessageService.saveMessage(chatMessage);
            chatMessage.setId(messageId);
            return R.ok().put("data", chatMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("保存消息失败");
        }
    }

    @GetMapping("/getHistory")
    public R getChatHistory(@RequestParam String consultBianhao, // 改为使用咨询编号
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "20") Integer size) {
        try {
            List<ChatMessage> messages = chatMessageService.getMessagesByConsultBianhao(consultBianhao);
            return R.ok().put("data", messages);
        } catch (Exception e) {
            return R.error("获取聊天记录失败");
        }
    }
}